using System;
using System.Collections.Generic;

namespace SeataNet.Core.Lock
{
    /// <summary>
    /// 锁状态
    /// </summary>
    public enum LockStatus
    {
        /// <summary>
        /// 已锁定
        /// </summary>
        Locked,

        /// <summary>
        /// 未锁定
        /// </summary>
        Unlocked,

        /// <summary>
        /// 锁超时
        /// </summary>
        Timeout
    }

    /// <summary>
    /// 全局锁
    /// </summary>
    public class GlobalLock
    {
        /// <summary>
        /// 锁ID
        /// </summary>
        public string LockId { get; set; }

        /// <summary>
        /// 资源ID
        /// </summary>
        public string ResourceId { get; set; }

        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 主键值
        /// </summary>
        public string PrimaryKey { get; set; }

        /// <summary>
        /// 锁持有的事务ID (XID)
        /// </summary>
        public string Xid { get; set; }

        /// <summary>
        /// 分支ID
        /// </summary>
        public long BranchId { get; set; }

        /// <summary>
        /// 锁状态
        /// </summary>
        public LockStatus Status { get; set; }

        /// <summary>
        /// 锁创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 锁过期时间
        /// </summary>
        public DateTime ExpireTime { get; set; }

        /// <summary>
        /// 构建锁Key
        /// </summary>
        public string GetLockKey()
        {
            return $"{ResourceId}:{TableName}:{PrimaryKey}";
        }

        /// <summary>
        /// 判断锁是否过期
        /// </summary>
        public bool IsExpired()
        {
            return DateTime.Now >= ExpireTime;
        }
    }

    /// <summary>
    /// 锁查询条件
    /// </summary>
    public class LockQuery
    {
        /// <summary>
        /// 资源ID
        /// </summary>
        public string ResourceId { get; set; }

        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 主键值列表
        /// </summary>
        public List<string> PrimaryKeys { get; set; } = new List<string>();

        /// <summary>
        /// 事务ID
        /// </summary>
        public string Xid { get; set; }

        /// <summary>
        /// 分支ID
        /// </summary>
        public long? BranchId { get; set; }
    }

    /// <summary>
    /// 锁冲突信息
    /// </summary>
    public class LockConflict
    {
        /// <summary>
        /// 冲突的锁Key
        /// </summary>
        public string LockKey { get; set; }

        /// <summary>
        /// 当前持有锁的事务ID
        /// </summary>
        public string CurrentXid { get; set; }

        /// <summary>
        /// 当前持有锁的分支ID
        /// </summary>
        public long CurrentBranchId { get; set; }

        /// <summary>
        /// 请求锁的事务ID
        /// </summary>
        public string RequestXid { get; set; }

        /// <summary>
        /// 请求锁的分支ID
        /// </summary>
        public long RequestBranchId { get; set; }

        /// <summary>
        /// 冲突时间
        /// </summary>
        public DateTime ConflictTime { get; set; }
    }

    /// <summary>
    /// 死锁检测结果
    /// </summary>
    public class DeadlockDetectionResult
    {
        /// <summary>
        /// 是否存在死锁
        /// </summary>
        public bool HasDeadlock { get; set; }

        /// <summary>
        /// 死锁涉及的事务ID列表
        /// </summary>
        public List<string> DeadlockXids { get; set; } = new List<string>();

        /// <summary>
        /// 死锁检测时间
        /// </summary>
        public DateTime DetectionTime { get; set; }

        /// <summary>
        /// 死锁描述
        /// </summary>
        public string Description { get; set; }
    }
}
